const pool = require("../connection/sqlPool");

async function GETONLINE(){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT * FROM `online_user`');
    return rows
}

async function INSERTONLINE(username,logo){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('INSERT INTO `online_user`(`username`, `logo`)VALUES(?,?)',[username,logo]);
    return rows;
}

async function DELETEONLINE(username){
    const promisePool = pool.promise();
    const [rows,fildes] = await promisePool.query('DELETE FROM `online_user` WHERE `username`= ?',[username])
    return rows;
}

async function ADDROOM(username){
    const promisePool = pool.promise();
    const[rows,fildes] = await promisePool.query('INSERT INTO `online_room`(`host`)VALUES(?)',[username]);
    return rows;
}

async function ADDTOPIC(username){
    const promisePool = pool.promise();
    const [rows_t,fildes] = await promisePool.query(
        'SELECT * FROM (SELECT * FROM `Subject_manage`ORDER BY RAND())AS temp GROUP BY `attribute`'
    )
    const [rows_a,fileds] = await promisePool.query('UPDATE `online_room`SET `topic` = ? WHERE `host`=?',[JSON.stringify(rows_t),username]);
    return rows_a
}

async function GETTOPIC(username){
    const promisePool = pool.promise();
    const [rows,fileds] = await promisePool.query('SELECT `topic` FROM `online_room` WHERE `host` =? OR `guest`=? ',[username,username]);
    return rows
}

async function ENTERROOM(username){
    const promisePool = pool.promise();
    const [rows,fildes] = await promisePool.query('UPDATE `online_room` SET `guest`= ? WHERE `guest` IS NULL OR `guest`=? LIMIT 1',[username,'']);
    const [rows_h,fildes_h] = await promisePool.query('SELECT * FROM `online_room` WHERE `guest`=?',[username]);
    return rows_h;
}

async function ADDHA(username,add_score){
    const promisePool = pool.promise();
    const [rows,fildes] = await promisePool.query('UPDATE `online_room` SET `answer_host`= `answer_host` + ? WHERE `host`=?',[add_score,username]);
    const [rows_n,fildes_n] = await promisePool.query('UPDATE `online_room` SET `answer_num`= `answer_num` + 1 WHERE `host`=?',[username]);
    
    
}

async function ADDGA(username,add_score){
    const promisePool = pool.promise();
    const [rows,fildes] = await promisePool.query('UPDATE `online_room` SET `answer_guest`= `answer_guest` + ? WHERE `guest`=?',[add_score,username]);
    const [rows_n,fildes_n] = await promisePool.query('UPDATE `online_room` SET `answer_num`= `answer_num` + 1 WHERE `guest`=?',[username]);
    //const [rows_s,fildes_s] = await promisePool.query('SELECT `answer_num` FROM `online_room`WHERE `host` = ?',[username]);
    return rows;
}

async function JUDGENEXT(username){
    const promisePool = pool.promise();
    const [rows_s,fildes_s] = await promisePool.query('SELECT `answer_num` FROM `online_room`WHERE `host` = ? OR `guest` = ?',[username,username]);
    if(rows_s[0].answer_num==2){
        const [rows_b,fildes_b] = await promisePool.query('UPDATE `online_room` SET `answer_num` = 0 WHERE `host` = ? OR `guest` = ?',[username,username]);
        const [rows_n,fildes_n] = await promisePool.query('UPDATE `online_room` SET `topic_no` = `topic_no` + 1 WHERE `host` = ? OR `guest` = ?',[username,username]);
        return {isComplete:1};
    }else{
        return {isComplete:0};
    }
}

async function GETROOM (username){
    const promisePool = pool.promise();
    const [rows_h,fildes_h] = await promisePool.query('SELECT * FROM `online_room` WHERE `guest`=? OR `host`=?',[username,username]);
    return rows_h[0];
}


module.exports = {GETONLINE,INSERTONLINE,DELETEONLINE,ADDROOM,ADDTOPIC,GETTOPIC,ENTERROOM,ADDHA,ADDGA,JUDGENEXT,GETROOM}